package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.Comment;

/**
 * A class used for managing comment
 */
public class CommentDAO {

	private Database db;

	public CommentDAO(Database db) {
		this.db = db;
	}

	public List<Map<String, Object>> findComment(int pnSiteId) {
		String vaSql = "SELECT comment_code, comment_description FROM comment_code WHERE site_id = ?";
		List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
		if (result != null) {
			return result;
		} else {
			return null;
		}
	}

	public String findDescriptionBySiteAndCommentCode(int pnSiteId,
			String paCommentCode) {
		String vaSql = "SELECT comment_description FROM comment_code WHERE site_id = ? and comment_code = ?";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId,
				paCommentCode);
		if (result != null) {
			return (String) result.get("COMMENT_DESCRIPTION");
		} else {
			return null;
		}
	}

	// get comment_code for check duplicate for insert data
	public String findCommentCodeBySiteAndComment(int pnSiteId,
			String paCommentCode) {
		String vaSql = "SELECT comment_code  FROM  comment_code WHERE site_id = ? and  comment_code = ? ";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId,
				paCommentCode);
		if (result != null) {
			return (String) result.get("COMMENT_CODE").toString();
		} else {
			return null;
		}
	}

	// get comment_code for check duplicate for update data
	public String findsCommentCodeBySiteAndCommentCode(int pnSiteId,
			String paOldCommentCode, String paNewCommentCode) {
		String vaSql = "SELECT comment_code FROM comment_code WHERE site_id = ? and comment_code != ? and comment_code = ?";
		Map<String, Object> result = db.querySingle(vaSql, pnSiteId,
				paOldCommentCode, paNewCommentCode);
		if (result != null) {
			return (String) result.get("COMMENT_CODE");
		} else {
			return null;
		}
	}

	// insert comment_code
	public void addCommentCode(Comment poCommentCode) {
		String vaSql = "INSERT INTO COMMENT_CODE (SITE_ID,COMMENT_CODE,COMMENT_DESCRIPTION,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,"
				+ "UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)"
				+ "VALUES(?,?,?,?,?,?,?,?,?)";
		try {
			db.insert(vaSql, poCommentCode.getVnSiteId(),
					poCommentCode.getVaCommentCode(),
					poCommentCode.getVaCommentDescription(),
					poCommentCode.getVnInsertUserId(),
					poCommentCode.getVaInsertDate(),
					poCommentCode.getVaInsertTime(),
					poCommentCode.getVnUpdateUserId(),
					poCommentCode.getVaUpdateDate(),
					poCommentCode.getVaUpdateTime());

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	// update comment_code
	public void updateCommentCode(int pnSiteId, String paCommentCode,
			Comment poCommentCode) {

		String vaSql = " UPDATE COMMENT_CODE SET " + " COMMENT_CODE = ? , "
				+ " COMMENT_DESCRIPTION  = ? , " + " UPDATE_USER_ID = ? , "
				+ " UPDATE_DATE = ? , " + " UPDATE_TIME = ? " + " WHERE "
				+ " COMMENT_CODE= ? " + " AND SITE_ID = ? ";

		try {
			db.update(vaSql, poCommentCode.getVaCommentCode(),
					poCommentCode.getVaCommentDescription(),
					poCommentCode.getVnUpdateUserId(),
					poCommentCode.getVaUpdateDate(),
					poCommentCode.getVaUpdateTime(), paCommentCode, pnSiteId);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	// delete comment_code
	public boolean deleteCommentCodeBySiteId(int pnSiteId, String paCommentCode) {
		String vaSql = "DELETE FROM COMMENT_CODE WHERE SITE_ID = ? and COMMENT_CODE = ?";
		try {
			db.update(vaSql, pnSiteId, paCommentCode);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
}
